﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using prjKQXS_DLL;
using prjKQXS_DLL.Ve_so;
using prjKQXS_DLL.Hop_dong;
using prjKQXS_Report.Boc;
using DevExpress.XtraSplashScreen;
using DevExpress.XtraReports.UI;
using DevExpress.XtraGrid;

namespace prjKQXS_UI.Boc
{
    public partial class frmQuayveboc2 : DevExpress.XtraEditors.XtraForm
    {
        private bool dong;
        public frmQuayveboc2()
        {
            InitializeComponent();
        }
        private Dictionary<string, string> lstQuaySo;
        private void frmQuayveboc2_Load(object sender, EventArgs e)
        {
            lstQuaySo = new Dictionary<string, string>()
            {
               {"txtnhat1",""},{"txtnhat2",""},{"txtnhat3",""},{"txtnhat4",""},
               {"txtnhi1",""},
               {"txtba1",""},
               {"txttu1",""},{"txttu2",""},
               {"txtnam1",""},{"txtnam2",""},{"txtnam3",""},{"txtnam4",""},
               {"txtnam5",""},{"txtnam6",""},{"txtnam7",""},{"txtnam8",""},
               {"txtnam9",""},{"txtnam10",""},{"txtnam11",""},{"txtnam12",""},
               {"txtnam13",""},{"txtnam14",""},{"txtnam15",""},{"txtnam16",""},
               {"txtnam17",""},{"txtnam18",""},
            };
            deNgay.DateTime = BienTc.denngay;
            txtgiave.Text = BienTc.GiaVe.ToString();
            txtkyhieu.Text = BienTc.KyHieuVe;
            txtnhat1.Mask = "0000";
            txtnhat2.Mask = "0000";
            txtnhat3.Mask = "0000";
            txtnhat4.Mask = "0000";
            txtnhi1.Mask = "0000";
            txtba1.Mask = "000";
            txttu1.Mask = "000";
            txttu2.Mask = "000";
            txtnam1.Mask = "00";
            txtnam2.Mask = "00";
            txtnam3.Mask = "00";
            txtnam4.Mask = "00";
            txtnam5.Mask = "00";
            txtnam6.Mask = "00";
            txtnam7.Mask = "00";
            txtnam8.Mask = "00";
            txtnam9.Mask = "00";
            txtnam10.Mask = "00";
            txtnam11.Mask = "00";
            txtnam12.Mask = "00";
            txtnam13.Mask = "00";
            txtnam14.Mask = "00";
            txtnam15.Mask = "00";
            txtnam16.Mask = "00";
            txtnam17.Mask = "00";
            txtnam18.Mask = "00";

            if (!dmcanbo_dal.Kiemtrakyhieuboc2(BienTc.KyHieuVe))
            {
                dong = false;
                Hienthiketqua();
                Thuoctinh();
            }
            else
            {
                txtnhat1.Focus();
                dong = true;
                Thuoctinh();
            }
        }

        private void Thuoctinh()
        {
            if (dong == false)
            {
                txtnhat1.Enabled = false;
                txtnhat2.Enabled = false;
                txtnhat3.Enabled = false;
                txtnhat4.Enabled = false;
                txtnhi1.Enabled = false;
                txtba1.Enabled = false;
                txttu1.Enabled = false;
                txttu2.Enabled = false;
                txtnam1.Enabled = false;
                txtnam2.Enabled = false;
                txtnam3.Enabled = false;
                txtnam4.Enabled = false;
                txtnam5.Enabled = false;
                txtnam6.Enabled = false;
                txtnam7.Enabled = false;
                txtnam8.Enabled = false;
                txtnam9.Enabled = false;
                txtnam10.Enabled = false;
                txtnam11.Enabled = false;
                txtnam12.Enabled = false;
                txtnam13.Enabled = false;
                txtnam14.Enabled = false;
                txtnam15.Enabled = false;
                txtnam16.Enabled = false;
                txtnam17.Enabled = false;
                txtnam18.Enabled = false;
            }
            else
            {
                txtnhat1.Enabled = true;
                txtnhat2.Enabled = true;
                txtnhat3.Enabled = true;
                txtnhat4.Enabled = true;
                txtnhi1.Enabled = true;
                txtba1.Enabled = true;
                txttu1.Enabled = true;
                txttu2.Enabled = true;
                txtnam1.Enabled = true;
                txtnam2.Enabled = true;
                txtnam3.Enabled = true;
                txtnam4.Enabled = true;
                txtnam5.Enabled = true;
                txtnam6.Enabled = true;
                txtnam7.Enabled = true;
                txtnam8.Enabled = true;
                txtnam9.Enabled = true;
                txtnam10.Enabled = true;
                txtnam11.Enabled = true;
                txtnam12.Enabled = true;
                txtnam13.Enabled = true;
                txtnam14.Enabled = true;
                txtnam15.Enabled = true;
                txtnam16.Enabled = true;
                txtnam17.Enabled = true;
                txtnam18.Enabled = true;
            }
        }
        private void Luuketqua()
        {
            try
            {

                DMVETT dmveboc = new DMVETT();
                dmveboc.Kyhieu = txtkyhieu.Text;
                dmveboc.Giave = 10000;
                //dmveboc.Giave = (Int32)clsCommon.Parse(txtgiave.Text, 0);
                dmveboc.Ngaymothuong = (DateTime)clsCommon.Parse(deNgay.Text, 4);
                dmveboc.gnhat1 = txtnhat1.Text.ToString().Trim();
                dmveboc.gnhat2 = txtnhat2.Text.ToString().Trim();
                dmveboc.gnhat3 = txtnhat3.Text.ToString().Trim();
                dmveboc.gnhat4 = txtnhat4.Text.ToString().Trim();
                dmveboc.gnhi1 = txtnhi1.Text.ToString().Trim();
                dmveboc.gba1 = txtba1.Text.ToString().Trim();
                dmveboc.gtu1 = txttu1.Text.ToString().Trim();
                dmveboc.gtu2 = txttu2.Text.ToString().Trim();
                dmveboc.gnam1 = txtnam1.Text.ToString().Trim();
                dmveboc.gnam2 = txtnam2.Text.ToString().Trim();
                dmveboc.gnam3 = txtnam3.Text.ToString().Trim();
                dmveboc.gnam4 = txtnam4.Text.ToString().Trim();
                dmveboc.gnam5 = txtnam5.Text.ToString().Trim();
                dmveboc.gnam6 = txtnam6.Text.ToString().Trim();
                dmveboc.gnam7 = txtnam7.Text.ToString().Trim();
                dmveboc.gnam8 = txtnam8.Text.ToString().Trim();
                dmveboc.gnam9 = txtnam9.Text.ToString().Trim();
                dmveboc.gnam10 = txtnam10.Text.ToString().Trim();
                dmveboc.gnam11 = txtnam11.Text.ToString().Trim();
                dmveboc.gnam12 = txtnam12.Text.ToString().Trim();
                dmveboc.gnam13 = txtnam13.Text.ToString().Trim();
                dmveboc.gnam14 = txtnam14.Text.ToString().Trim();
                dmveboc.gnam15 = txtnam15.Text.ToString().Trim();
                dmveboc.gnam16 = txtnam16.Text.ToString().Trim();
                dmveboc.gnam17 = txtnam17.Text.ToString().Trim();
                dmveboc.gnam18 = txtnam18.Text.ToString().Trim();
                dmcanbo_dal.InsertDMVEBOC1(dmveboc);
            }
            catch
            {

            }
        }

        private void Hienthiketqua()
        {
            try
            {
                DMVETT dmveboc = new DMVETT();
                dmveboc.Kyhieu = txtkyhieu.Text;
                dmveboc.Giave = (Int32)clsCommon.Parse(txtgiave.Text, 0);
                dmveboc.Ngaymothuong = (DateTime)clsCommon.Parse(deNgay.Text, 4);
                txtnhat1.Text = dmcanbo_dal.SelectDMVEBOC2("gnhat1", dmveboc.Kyhieu);
                txtnhat2.Text = dmcanbo_dal.SelectDMVEBOC2("gnhat2", dmveboc.Kyhieu);
                txtnhat3.Text = dmcanbo_dal.SelectDMVEBOC2("gnhat3", dmveboc.Kyhieu);
                txtnhat4.Text = dmcanbo_dal.SelectDMVEBOC2("gnhat4", dmveboc.Kyhieu);
                txtnhi1.Text = dmcanbo_dal.SelectDMVEBOC2("gnhi1", dmveboc.Kyhieu);
                txtba1.Text = dmcanbo_dal.SelectDMVEBOC2("gba1", dmveboc.Kyhieu);
                txttu1.Text = dmcanbo_dal.SelectDMVEBOC2("gtu1", dmveboc.Kyhieu);
                txttu2.Text = dmcanbo_dal.SelectDMVEBOC2("gtu2", dmveboc.Kyhieu);
                txtnam1.Text = dmcanbo_dal.SelectDMVEBOC2("gnam1", dmveboc.Kyhieu);
                txtnam2.Text = dmcanbo_dal.SelectDMVEBOC2("gnam2", dmveboc.Kyhieu);
                txtnam3.Text = dmcanbo_dal.SelectDMVEBOC2("gnam3", dmveboc.Kyhieu);
                txtnam4.Text = dmcanbo_dal.SelectDMVEBOC2("gnam4", dmveboc.Kyhieu);
                txtnam5.Text = dmcanbo_dal.SelectDMVEBOC2("gnam5", dmveboc.Kyhieu);
                txtnam6.Text = dmcanbo_dal.SelectDMVEBOC2("gnam6", dmveboc.Kyhieu);
                txtnam7.Text = dmcanbo_dal.SelectDMVEBOC2("gnam7", dmveboc.Kyhieu);
                txtnam8.Text = dmcanbo_dal.SelectDMVEBOC2("gnam8", dmveboc.Kyhieu);
                txtnam9.Text = dmcanbo_dal.SelectDMVEBOC2("gnam9", dmveboc.Kyhieu);
                txtnam10.Text = dmcanbo_dal.SelectDMVEBOC2("gnam10", dmveboc.Kyhieu);
                txtnam11.Text = dmcanbo_dal.SelectDMVEBOC2("gnam11", dmveboc.Kyhieu);
                txtnam12.Text = dmcanbo_dal.SelectDMVEBOC2("gnam12", dmveboc.Kyhieu);
                txtnam13.Text = dmcanbo_dal.SelectDMVEBOC2("gnam13", dmveboc.Kyhieu);
                txtnam14.Text = dmcanbo_dal.SelectDMVEBOC2("gnam14", dmveboc.Kyhieu);
                txtnam15.Text = dmcanbo_dal.SelectDMVEBOC2("gnam15", dmveboc.Kyhieu);
                txtnam16.Text = dmcanbo_dal.SelectDMVEBOC2("gnam16", dmveboc.Kyhieu);
                txtnam17.Text = dmcanbo_dal.SelectDMVEBOC2("gnam17", dmveboc.Kyhieu);
                txtnam18.Text = dmcanbo_dal.SelectDMVEBOC2("gnam18", dmveboc.Kyhieu);
            }
            catch
            {

            }
        }
  private DMVETT_DAL dmcanbo_dal = new DMVETT_DAL();
        private void rpt_OnLoadComplete()
        {
        //    SplashScreenManager.CloseForm();
        }
            void Kiemtraso(string So)
        {
            int d1 = 0;
            int d2 = 0;
            int d3 = 0;
            int m = 0;
            string bien1 = "";
            string bien2 = "";
            string bien3 = "";
            string[] A = new string[27];
            A[1] = txtnhat1.Text.Trim();
            A[2] = txtnhat2.Text.Trim();
            A[3] = txtnhat3.Text.Trim();
            A[4] = txtnhat4.Text.Trim();
            A[5] = txtnhi1.Text.Trim();
            A[6] = txtba1.Text.Trim();
            A[7] = txttu1.Text.Trim();
            A[8] = txttu2.Text.Trim();
            A[9] = txtnam1.Text.Trim();
            A[10] = txtnam2.Text.Trim();
            A[11] = txtnam3.Text.Trim();
            A[12] = txtnam4.Text.Trim();
            A[13] = txtnam5.Text.Trim();
            A[14] = txtnam6.Text.Trim();
            A[15] = txtnam7.Text.Trim();
            A[16] = txtnam8.Text.Trim();
            A[17] = txtnam9.Text.Trim();
            A[18] = txtnam10.Text.Trim();
            A[19] = txtnam11.Text.Trim();
            A[20] = txtnam12.Text.Trim();
            A[21] = txtnam13.Text.Trim();
            A[22] = txtnam14.Text.Trim();
            A[23] = txtnam15.Text.Trim();
            A[24] = txtnam16.Text.Trim();
            A[25] = txtnam17.Text.Trim();
            A[26] = txtnam18.Text.Trim();
            if (So.Length == 4)
            {
                bien1 = So;
                bien2 = So.Substring(1, 3);
                bien3 = So.Substring(2, 2);
            }
            if (So.Length == 3)
            {
                bien2 = So;
                bien3 = So.Substring(1, 2);
            }
            if (So.Length == 2)
            {
                bien3 = So;
            }

            if (bien3 != null || bien3 != "")
            {
                for (int i = 1; i < A.Length; ++i)
                {
                    if (A[i] != "" && A[i].Length >= 2)
                    {
                        if (A[i].Length == 4)
                            m = 2;
                        else
                            if (A[i].Length == 3)
                                m = 1;
                            else
                                m = 0;

                        if (bien3 == A[i].Substring(m, 2))
                        {
                            d3 = d3 + 1;
                        }
                    }
                }
            }
            if (bien2 != null || bien2 != "")
            {
                for (int i = 1; i < A.Length; ++i)
                {
                    if (A[i] != "" && A[i].Length >= 3)
                    {
                        if (A[i].Length == 4)
                            m = 1;
                        else
                            if (A[i].Length == 3)
                                m = 0;
                        if (bien2 == A[i].Substring(m, 3))
                        {
                            d2 = d2 + 1;
                        }
                    }
                }
            }
            if (bien1 != null || bien1 != "")
            {
                for (int i = 1; i < A.Length; ++i)
                {
                    if (A[i] != "" && A[i].Length >= 4)
                    {
                        if (bien1 == A[i].Substring(0, 4))
                        {
                            d1 = d1 + 1;
                        }
                    }
                }
            }
            if (d1 >= 2 || d2 >= 2 || d3 >= 2)
            {
                clsCommon.MsgBox("Số này đã quay rồi, đề nghị quay số khác!", 0);
                clsCommon.MsgBox("Đề nghị quay lại số khác!", 1);
            }
        }
        private bool Kiemtraluu()
        {
            bool kq = false;
            string[] A = new string[30];
            A[1] = txtnhat1.Text.Trim();
            A[2] = txtnhat2.Text.Trim();
            A[3] = txtnhat3.Text.Trim();
            A[4] = txtnhat4.Text.Trim();
            A[5] = txtnhi1.Text.Trim();
            A[6] = txtba1.Text.Trim();
            A[7] = txttu1.Text.Trim();
            A[8] = txttu2.Text.Trim();
            A[9] = txtnam1.Text.Trim();
            A[10] = txtnam2.Text.Trim();
            A[11] = txtnam3.Text.Trim();
            A[12] = txtnam4.Text.Trim();
            A[13] = txtnam5.Text.Trim();
            A[14] = txtnam6.Text.Trim();
            A[15] = txtnam7.Text.Trim();
            A[16] = txtnam8.Text.Trim();
            A[17] = txtnam9.Text.Trim();
            A[18] = txtnam10.Text.Trim();
            A[19] = txtnam11.Text.Trim();
            A[20] = txtnam12.Text.Trim();
            A[21] = txtnam13.Text.Trim();
            A[22] = txtnam14.Text.Trim();
            A[23] = txtnam15.Text.Trim();
            A[24] = txtnam13.Text.Trim();
            A[25] = txtnam14.Text.Trim();
            A[26] = txtnam15.Text.Trim();
            A[27] = txtnam16.Text.Trim();
            A[28] = txtnam17.Text.Trim();
            A[29] = txtnam18.Text.Trim();
            for (int i = 1; i < 6; ++i)
            {
                if (A[i] == "" || A[i].Length < 4)
                {
                    kq = true;
                }
            }
            for (int i = 6; i < 9; ++i)
            {
                if (A[i] == "" || A[i].Length < 3)
                {
                    kq = true;
                }
            }
            for (int i = 9; i < 30; ++i)
            {
                if (A[i] == "" || A[i].Length < 2)
                {
                    kq = true;
                }
            }       
            return kq;
        }
        private void txtnhat1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode.ToString() == "Down" || e.KeyCode == Keys.Enter)
            {
                txtnhat2.Focus();
                if (txtnhat1.Text.Trim() != "")
                {
                    Kiemtraso(txtnhat1.Text.Trim());
                }
            }
        }

        private void txtnhat2_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode.ToString() == "Down" || e.KeyCode == Keys.Enter)
            {
                txtnhat3.Focus();
                if (txtnhat2.Text.Trim() != "")
                {
                    Kiemtraso(txtnhat2.Text.Trim());
                }
            }
            if (e.KeyCode.ToString() == "Up")
                txtnhat1.Focus();
        }

        private void txtnhat3_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode.ToString() == "Down" || e.KeyCode == Keys.Enter)
            {
                txtnhat4.Focus();
                if (txtnhat3.Text.Trim() != "")
                {
                    Kiemtraso(txtnhat3.Text.Trim());
                }
            }
            if (e.KeyCode.ToString() == "Up")
                txtnhat2.Focus();
        }

        private void txtnhat4_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode.ToString() == "Down" || e.KeyCode == Keys.Enter)
            {
                txtnhi1.Focus();
                if (txtnhat4.Text.Trim() != "")
                {
                    Kiemtraso(txtnhat4.Text.Trim());
                }
            }
            if (e.KeyCode.ToString() == "Up")
                txtnhat3.Focus();
        }

        private void txtnhi1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode.ToString() == "Down" || e.KeyCode == Keys.Enter)
            {
                txtba1.Focus();
                if (txtnhi1.Text.Trim() != "")
                {
                    Kiemtraso(txtnhi1.Text.Trim());
                }
            }
            if (e.KeyCode.ToString() == "Up")
                txtnhat4.Focus();
        }

        private void txtba1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode.ToString() == "Down" || e.KeyCode == Keys.Enter)
            {
                txttu1.Focus();
                if (txtba1.Text.Trim() != "")
                {
                    Kiemtraso(txtba1.Text.Trim());
                }
            }
            if (e.KeyCode.ToString() == "Up")
                txtnhi1.Focus();
        }

        private void txttu1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode.ToString() == "Down" || e.KeyCode == Keys.Enter)
            {
                txttu2.Focus();
                if (txttu1.Text.Trim() != "")
                {
                    Kiemtraso(txttu1.Text.Trim());
                }
            }
            if (e.KeyCode.ToString() == "Up")
                txtba1.Focus();
        }

        private void txttu2_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode.ToString() == "Down" || e.KeyCode == Keys.Enter)
            {
                txtnam1.Focus();
                if (txttu2.Text.Trim() != "")
                {
                    Kiemtraso(txttu2.Text.Trim());
                }
            }
            if (e.KeyCode.ToString() == "Up")
                txttu1.Focus();
        }

        private void txtnam1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode.ToString() == "Down" || e.KeyCode == Keys.Enter)
            {
                txtnam2.Focus();
                if (txtnam1.Text.Trim() != "")
                {
                    Kiemtraso(txtnam1.Text.Trim());
                }
            }
            if (e.KeyCode.ToString() == "Up")
                txttu2.Focus();
        }

        private void txtnam2_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode.ToString() == "Down" || e.KeyCode == Keys.Enter)
            {
                txtnam3.Focus();
                if (txtnam2.Text.Trim() != "")
                {
                    Kiemtraso(txtnam2.Text.Trim());
                }
            }
            if (e.KeyCode.ToString() == "Up")
                txtnam1.Focus();
        }

        private void txtnam3_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode.ToString() == "Down" || e.KeyCode == Keys.Enter)
            {
                txtnam4.Focus();
                if (txtnam3.Text.Trim() != "")
                {
                    Kiemtraso(txtnam3.Text.Trim());
                }
            }
            if (e.KeyCode.ToString() == "Up")
                txtnam2.Focus();
        }

        private void txtnam4_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode.ToString() == "Down" || e.KeyCode == Keys.Enter)
            {
                txtnam5.Focus();
                if (txtnam4.Text.Trim() != "")
                {
                    Kiemtraso(txtnam4.Text.Trim());
                }
            }
            if (e.KeyCode.ToString() == "Up")
                txtnam3.Focus();
        }

        private void txtnam5_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode.ToString() == "Down" || e.KeyCode == Keys.Enter)
            {
                txtnam6.Focus();
                if (txtnam5.Text.Trim() != "")
                {
                    Kiemtraso(txtnam5.Text.Trim());
                }
            }
            if (e.KeyCode.ToString() == "Up")
                txtnam4.Focus();
        }

        private void txtnam6_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode.ToString() == "Down" || e.KeyCode == Keys.Enter)
            {
                txtnam7.Focus();
                if (txtnam6.Text.Trim() != "")
                {
                    Kiemtraso(txtnam6.Text.Trim());
                }
            }
            if (e.KeyCode.ToString() == "Up")
                txtnam5.Focus();
        }

        private void txtnam7_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode.ToString() == "Down" || e.KeyCode == Keys.Enter)
            {
                txtnam8.Focus();
                if (txtnam7.Text.Trim() != "")
                {
                    Kiemtraso(txtnam7.Text.Trim());
                }
            }
            if (e.KeyCode.ToString() == "Up")
                txtnam6.Focus();
        }

        private void txtnam8_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode.ToString() == "Down" || e.KeyCode == Keys.Enter)
            {
                txtnam9.Focus();
                if (txtnam8.Text.Trim() != "")
                {
                    Kiemtraso(txtnam8.Text.Trim());
                }
            }
            if (e.KeyCode.ToString() == "Up")
                txtnam7.Focus();
        }

        private void txtnam9_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode.ToString() == "Down" || e.KeyCode == Keys.Enter)
            {
                txtnam10.Focus();
                if (txtnam9.Text.Trim() != "")
                {
                    Kiemtraso(txtnam9.Text.Trim());
                }
            }
            if (e.KeyCode.ToString() == "Up")
                txtnam8.Focus();
        }

        private void txtnam10_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode.ToString() == "Down" || e.KeyCode == Keys.Enter)
            {
                txtnam11.Focus();
                if (txtnam10.Text.Trim() != "")
                {
                    Kiemtraso(txtnam10.Text.Trim());
                }
            }
            if (e.KeyCode.ToString() == "Up")
                txtnam9.Focus();
        }

        private void txtnam11_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode.ToString() == "Down" || e.KeyCode == Keys.Enter)
            {
                txtnam12.Focus();
                if (txtnam11.Text.Trim() != "")
                {
                    Kiemtraso(txtnam11.Text.Trim());
                }
            }
            if (e.KeyCode.ToString() == "Up")
                txtnam10.Focus();
        }

        private void txtnam12_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode.ToString() == "Down" || e.KeyCode == Keys.Enter)
            {
                txtnam13.Focus();
                if (txtnam12.Text.Trim() != "")
                {
                    Kiemtraso(txtnam12.Text.Trim());
                }
            }
            if (e.KeyCode.ToString() == "Up")
                txtnam11.Focus();
        }

        private void txtnam13_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode.ToString() == "Down" || e.KeyCode == Keys.Enter)
            {
                txtnam14.Focus();
                if (txtnam13.Text.Trim() != "")
                {
                    Kiemtraso(txtnam13.Text.Trim());
                }
            }
            if (e.KeyCode.ToString() == "Up")
                txtnam12.Focus();
        }

        private void txtnam14_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode.ToString() == "Down" || e.KeyCode == Keys.Enter)
            {
                txtnam15.Focus();
                if (txtnam14.Text.Trim() != "")
                {
                    Kiemtraso(txtnam14.Text.Trim());
                }
            }
            if (e.KeyCode.ToString() == "Up")
                txtnam13.Focus();
        }

        private void txtnam15_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode.ToString() == "Down" || e.KeyCode == Keys.Enter)
            {
                txtnam16.Focus();
                if (txtnam15.Text.Trim() != "")
                {
                    Kiemtraso(txtnam15.Text.Trim());
                }
            }
            if (e.KeyCode.ToString() == "Up")
                txtnam14.Focus();
        }

        private void txtnam16_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode.ToString() == "Down" || e.KeyCode == Keys.Enter)
            {
                txtnam17.Focus();
                if (txtnam16.Text.Trim() != "")
                {
                    Kiemtraso(txtnam16.Text.Trim());
                }
            }
            if (e.KeyCode.ToString() == "Up")
                txtnam15.Focus();
        }

        private void txtnam17_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode.ToString() == "Down" || e.KeyCode == Keys.Enter)
            {
                txtnam18.Focus();
                if (txtnam17.Text.Trim() != "")
                {
                    Kiemtraso(txtnam17.Text.Trim());
                }
            }
            if (e.KeyCode.ToString() == "Up")
                txtnam16.Focus();
        }

        private void txtnam18_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode.ToString() == "Down" || e.KeyCode == Keys.Enter)
            {
                btnin.Focus();
                if (txtnam18.Text.Trim() != "")
                {
                    Kiemtraso(txtnam18.Text.Trim());
                }   
            }
            if (e.KeyCode.ToString() == "Up")
                txtnam17.Focus();
        }

        private void btndong_Click(object sender, EventArgs e)
        {
            if (!Kiemtraluu())
            {
                Luuketqua();
            }
            this.Close();
        }

        private void btnin_Click(object sender, EventArgs e)
        {
            if (!Kiemtraluu())
            {
                Luuketqua();
                rptbienbanboc2 rpt = new rptbienbanboc2();               
                rpt.ShowRibbonPreviewDialog();
            }
            else clsCommon.MsgBox("Kết quả bạn nhập vào bị thiếu", 1);
        }
        private void checktrungNhapSo(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                // Enter key pressed (nhập giải xong enter sang giải khác)

                MaskedTextBox txt = (MaskedTextBox)sender;
                if (txt.Text.Length >= 2)
                {
                    string value = txt.Text.Substring(txt.Text.Length - 2, 2);
                    bool isValid = true;
                    foreach (var pair in lstQuaySo)
                    {
                        if (pair.Key != txt.Name && pair.Value == value)
                        {
                            isValid = false;
                            break;
                        }
                    }
                    if (isValid)
                    {
                        lstQuaySo[txt.Name] = value;
                        SendKeys.Send("{TAB}");
                    }
                    else
                    {
                        MessageBox.Show("Không được nhập 2 số cuối trùng");
                        txt.Text = "";
                        txt.Focus();
                    }
                }
            }
        }

        
    }      
}
